package br.com.tcc.systemdx.web.controllers;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import br.com.tcc.systemdx.domain.AtributoGenerico;
import br.com.tcc.systemdx.domain.AtributoSintoma;
import br.com.tcc.systemdx.domain.ValorGenerico;
import br.com.tcc.systemdx.persistence.AtributoSintomaDAO;
import br.com.tcc.systemdx.persistence.ValorSintomaDAO;
import br.com.tcc.systemdx.service.MecanismoInferenciaService;
import br.com.tcc.systemdx.service.beans.QuestaoHipotese;
import br.com.tcc.systemdx.web.editors.AtributoSintomaPropertyEditor;
import br.com.tcc.systemdx.web.editors.ValorSintomaPropertyEditor;

@Controller
@Scope("session")
public class CicloHipoteseController implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4009365515700557844L;
	/**
	 * 
	 */
	@Autowired
	private MecanismoInferenciaService mecanismoInferenciaService;
	@Autowired
	private AtributoSintomaDAO atributoSintomaDAO;
	@Autowired
	private ValorSintomaDAO valorSintomaDAO;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(ValorGenerico.class, new ValorSintomaPropertyEditor(valorSintomaDAO));
		binder.registerCustomEditor(AtributoGenerico.class, new AtributoSintomaPropertyEditor(atributoSintomaDAO));
	}
	
	@RequestMapping("/inicio.htm")
	public ModelMap inicioHandler() {
		mecanismoInferenciaService.inicializarCicloTesteHipoteses();
		return new ModelMap(atributoSintomaDAO.findAll());
	}
	
	@RequestMapping("/selecaoValor.htm")
	public ModelMap selecaoValorHandler(@RequestParam(value = "atributoId") Long atributoId) {
		AtributoSintoma atributo = atributoSintomaDAO.findById(atributoId);
		ModelMap model = new ModelMap(atributo.getValores());
		model.addAttribute("atributoId", atributoId);
		return model;
	}
	
	@RequestMapping(value = "/cicloHipotese.htm", method = RequestMethod.GET)
	public String cicloHipoteseSetupFormHandler(@RequestParam(value = "atributoId", required = false) Long atributoId, 
			@RequestParam(value = "valorId", required = false)Long valorId, ModelMap model)  {
		
		QuestaoHipotese questaoHipotese = null;
		
		if (atributoId != null && valorId != null) {
			// Se estiver recebendo atributoId e valorId é pq precisa-se inicializar o mecanismo
			mecanismoInferenciaService.inicializarCicloTesteHipoteses(atributoId, valorId);
		}
		questaoHipotese = mecanismoInferenciaService.obterNovaQuestaoHipoteseSintoma();
		model.addAttribute("foco", mecanismoInferenciaService.getFoco());
		model.addAttribute("manifestacoes", mecanismoInferenciaService.getManifestacoes());
		model.addAttribute("escopo", mecanismoInferenciaService.getEscopoDiagnostico());
		if (questaoHipotese != null) {
			model.addAttribute("questaoHipotese", questaoHipotese);
			return "cicloHipotese";
		} else {
			return "redirect:cicloHipoteseFinalizado.htm";
		}
	}
	
	@RequestMapping(value = "/cicloHipotese.htm", method = RequestMethod.POST)
	public String cicloHipoteseProcessSubmit(@ModelAttribute("questaoHipotese") QuestaoHipotese questaoHipotese) {
		if (questaoHipotese != null && questaoHipotese.getResposta() != null) {
			mecanismoInferenciaService.responderQuestaoHipoteseSintoma(questaoHipotese);
		}
		return "redirect:cicloHipotese.htm";
	}
	
	@RequestMapping("/cicloHipoteseFinalizado.htm")
	public ModelMap cicloHipoteseFinalizadoHandler() {
		ModelMap model = new ModelMap();
		model.addAttribute("foco", mecanismoInferenciaService.getFoco());
		model.addAttribute("manifestacoes", mecanismoInferenciaService.getManifestacoes());
		model.addAttribute("escopo", mecanismoInferenciaService.getEscopoDiagnostico());
		return model;
	}

}
